﻿#include "ChineseHistoryTools.h"
#include <QDebug>

ChineseHistoryTools* ChineseHistoryTools::m_chineseHistoryTool = 0;
ChineseHistoryTools *ChineseHistoryTools::GetInstance()
{
    if(m_chineseHistoryTool == 0)
        m_chineseHistoryTool = new ChineseHistoryTools;
    return m_chineseHistoryTool;
}

void ChineseHistoryTools::initMainUI()
{
    m_chineseHistoryIndex = new ChineseHistoryIndex;
    m_chineseHistoryIndex->show();
}

// 解析json，获取历史数据信息
void ChineseHistoryTools::getChinexeHistoryData(QString path)
{
    QFile file(path);
    if (!file.open(QIODevice::ReadWrite))
    {
        qDebug() << __FUNCTION__ << __LINE__ << "文件打开失败!";
        return;
    }
    m_countryInfo.clear();

    QJsonParseError jsonParserError;
    QJsonDocument jsonDocument = QJsonDocument::fromJson(file.readAll(), &jsonParserError);

    if (!jsonDocument.isNull() && jsonParserError.error == QJsonParseError::NoError )
    {
        qDebug() << __FUNCTION__ << __LINE__ << "文件解析成功";
        if (jsonDocument.isObject())   // 判断是否为对象 -- 第一级的 {}
        {
            QJsonObject jsonObject = jsonDocument.object();
            if(jsonObject.contains("countyies") && jsonObject.value("countyies").isArray())  // 第二级 -- "countyies"
            {
                QJsonArray jsonArray = jsonObject.value("countyies").toArray();
                for (int i = 0; i < jsonArray.size(); ++i)  // 遍历 "countries" 下所有数组（国家信息）
                {
                    Country *ptrCountry = new Country();
                    ptrCountry->emperors_list.clear();
                    if (jsonArray[i].isObject())
                    {
                        QJsonObject jsonObjectCountry = jsonArray[i].toObject(); // 第三级 -- {国家信息}
                        if( jsonObjectCountry.contains("country") && jsonObjectCountry.value("country").isString() &&
                                jsonObjectCountry.contains("start_time") && jsonObjectCountry.value("start_time").isDouble() &&
                                jsonObjectCountry.contains("stop_time") && jsonObjectCountry.value("stop_time").isDouble())
                        {
                            ptrCountry->country_name = jsonObjectCountry.value("country").toString();
                            ptrCountry->start_time = jsonObjectCountry.value("start_time").toInt();
                            ptrCountry->stop_time = jsonObjectCountry.value("stop_time").toInt();
                        }
                        if(jsonObjectCountry.contains("governors") && jsonObjectCountry.value("governors").isArray())
                        {
                            QJsonArray jsonArrayGovernor = jsonObjectCountry.value("governors").toArray();  // 第四级 -- 统治者信息
                            for(int j = 0; j < jsonArrayGovernor.size(); ++j)
                            {
                                Emperor *ptrEmperor = new Emperor();
                                if(jsonArrayGovernor[j].isObject())
                                {
                                    QJsonObject jsonObjectEmperor = jsonArrayGovernor[j].toObject();  // 第五级 -- 具体每一个皇帝信息
                                    if( jsonObjectEmperor.contains("emperor") && jsonObjectEmperor.value("emperor").isString() &&
                                            jsonObjectEmperor.contains("begin_time") && jsonObjectEmperor.value("begin_time").isDouble() &&
                                            jsonObjectEmperor.contains("end_time") && jsonObjectEmperor.value("end_time").isDouble())
                                    {
                                        ptrEmperor->emperor_name = jsonObjectEmperor.value("emperor").toString();
                                        ptrEmperor->begin_time = jsonObjectEmperor.value("begin_time").toInt();
                                        ptrEmperor->end_time = jsonObjectEmperor.value("end_time").toInt();
                                    }
                                }
                                ptrCountry->emperors_list.append(ptrEmperor);
                            }
                        }
                    }
                    m_countryInfo.append(ptrCountry);
                }
            }
        }
    }
    file.close();
    qDebug() << "m_countryInfo.size()" << m_countryInfo.size();
}

// 返回指定朝代各个皇帝的在位时间
QList<EmperorReginTime> ChineseHistoryTools::getEmperorReginTime(QString country)
{
    QList<EmperorReginTime> lst;
    lst.clear();
    for(int i = 0; i < m_countryInfo.size(); ++i)
    {
        if(m_countryInfo.at(i)->country_name == country)
        {

            QList<Emperor *> targetLst = m_countryInfo.at(i)->emperors_list;  // 当前国家的皇帝信息
            for(int j = 0; j < targetLst.size(); ++j)
            {
                for(int m = 0; m < lst.size(); ++m)  // 同一个皇帝可能在位多次，在位时间要累加
                {
                    if(lst.at(m).name == targetLst.at(j)->emperor_name)
                    {
                        lst[m].time += (targetLst.at(j)->end_time - targetLst.at(j)->begin_time);
                        break;
                    }
                }

                EmperorReginTime temp;
                temp.name = targetLst.at(j)->emperor_name;
                temp.time = targetLst.at(j)->end_time - targetLst.at(j)->begin_time;
                lst.append(temp);
            }
        }
    }

    return lst;
}

// 返回指定时间段内所有国家信息 详细
QList<Country> ChineseHistoryTools::getUnSimppleCountryInfo(int beginTime, int endTime)
{
    QList<Country> lst;
    lst.clear();
    for(int i = 0; i < m_countryInfo.size(); ++i)
    {
        Country country;
        if(!(m_countryInfo.at(i)->start_time >= endTime || m_countryInfo.at(i)->stop_time <= beginTime))
        {
            country.country_name = m_countryInfo.at(i)->country_name;
            country.start_time = (beginTime >= m_countryInfo.at(i)->start_time) ? beginTime : m_countryInfo.at(i)->start_time;
            country.stop_time = (endTime <= m_countryInfo.at(i)->stop_time) ? endTime : m_countryInfo.at(i)->stop_time;

            QList<Emperor *> lst_emperor;
            lst_emperor.clear();
            for(int j = 0; j < m_countryInfo.at(i)->emperors_list.size(); ++j)
            {
                if(!(m_countryInfo.at(i)->emperors_list.at(j)->begin_time >= endTime || m_countryInfo.at(i)->emperors_list.at(j)->end_time <= beginTime))
                {
                    Emperor *emperor = new Emperor();
                    emperor->emperor_name = m_countryInfo.at(i)->emperors_list.at(j)->emperor_name;
                    emperor->begin_time = (beginTime <= m_countryInfo.at(i)->emperors_list.at(j)->begin_time) ? beginTime : m_countryInfo.at(i)->emperors_list.at(j)->end_time;
                    emperor->end_time = (endTime <= m_countryInfo.at(i)->emperors_list.at(j)->end_time) ? endTime : m_countryInfo.at(i)->emperors_list.at(j)->end_time;
                    lst_emperor.append(emperor);
                }
            }

            country.emperors_list = lst_emperor;
            lst.append(country);
        }
    }
    return lst;
}

// 返回指定时间段内所有国家信息 简略 {国家 起止时间}
QList<CountryInfo> ChineseHistoryTools::getSimpleCountryInfo(QString country, QString emperor)
{
    int start = 0;
    int end = 0;
    QList<CountryInfo> lst;
    QList<Country> lst_country;
    lst_country.clear();
    for(int i = 0; i < m_countryInfo.size(); ++i)
    {
        if(m_countryInfo.at(i)->country_name == country)
        {
            for(int j = 0; j < m_countryInfo.at(i)->emperors_list.size(); ++j)
            {
                if(m_countryInfo.at(i)->emperors_list.at(j)->emperor_name == emperor)
                {
                    start = m_countryInfo.at(i)->emperors_list.at(j)->begin_time;  // 起始时间
                    end = m_countryInfo.at(i)->emperors_list.at(j)->end_time;  // 结束时间
//                    qDebug() << __FUNCTION__ << __LINE__ << "起始时间 = " << start;
//                    qDebug() << __FUNCTION__ << __LINE__ << "结束时间 = " << end;
                    // 返回当前时间段的所有国家信息 详细
                    lst_country = getUnSimppleCountryInfo(start, end);
                    break;
                }
            }
        }
    }

    lst.clear();
    for(int i = 0; i < lst_country.size(); ++i)
    {
        CountryInfo info;
        info.name = lst_country.at(i).country_name;
        info.start_time = lst_country.at(i).start_time;
        info.end_time = lst_country.at(i).stop_time;
        lst.append(info);
    }

    return lst;
}

// 返回所有朝代名称列表
QStringList ChineseHistoryTools::getCountryList()
{
    QStringList lst;
    for(int i = 0; i < m_countryInfo.size(); ++i)
        lst << m_countryInfo.at(i)->country_name;

    return lst;
}

